//设置全局AJAX默认选项。
$.ajaxSetup({cache : false});
//jQuery常用函数封装
(function ($){
    //删除
    $.fn.isDel = function (){
        return this.each(function() {
            $(this).click(function(){
                var url = $(this).attr("url");
                var css = this;
                parent.layer.confirm('确认要删除本资源?',{icon: 3,title:'友情提示'}, function(index){
                    $.getJSON(url,function(data) {
                        parent.layer.close(index);
                        if (data.code == 200) {
                            $(css).parents("tr").remove();
                            $(css).parent().remove();
                            $.isEmptyObject(data.url) ? '' : window.location.replace(data.url);
                        }else{
                            parent.layer.msg(data.msg,{icon:5,shade:0.5})
                        }
                    });
                });
            })
        });
    };
    //操作某URL并返回JSON
    $.fn.actUrl = function (options) {
        var defaults = {
            msg:'确认要操作当前资源?',
            parent:false
        }
        var options = $.extend(defaults, options);
        return this.each(function() {
            $(this).click(function () {
                var url = $(this).attr("url");
                parent.layer.confirm(options.msg,{icon: 3,title: '友情提示'}, function (index){
                    parent.layer.close(index);
                    var load = parent.layer.load(0,{shade:[0.3,'#393D49'],time:3000});
                    $.getJSON(url, function (data) {
                        if (data.code == 200) {
                            if (options.parent) {
                                $.isEmptyObject(data.url) ? window.parent.location.reload() : window.parent.location.replace(data.url);
                            } else {
                                $.isEmptyObject(data.url) ? window.location.reload() : window.location.replace(data.url);
                            }
                        }else{
                            parent.layer.msg(data.msg,{icon:5,shade:0.5})
                        }
                        parent.layer.close(load);
                    });
                });
            })
        });
    };
    //弹出窗口
    $.fn.win = function (options){
        var defaults = {
            url: $(this).attr("url"),
            area:['60%','70%'],
            reload: 0
        }
        var options  = $.extend(defaults,options);
        return this.each(function () {
            parent.layer.open({type: 2,title:'快捷窗口',maxmin:true,area:options.area,content: options.url,cancel:function (index){
                parent.layer.close(index);
                if (options.reload == 1) { 
                    parent.$("#iframe")[0].contentWindow.location.reload()
                }
            }});
        });
    };
    //弹出窗口
    $.fn.popup = function (url){
        var options = {
            input: $(this).attr('data') ? $(this).attr('data') : $(this).attr('id'),
            area: ['60%', '70%'],
            url: url
        }
        return this.each(function() {
            if(options.input != undefined){
                options.url = options.url+'?input=' + options.input;
            }
            parent.layer.open({
                type: 2,title:'快捷窗口',maxmin:true,area:options.area,content:options.url
            });
        });
    };
    //表单
    $.fn.isForm = function (options){
        var defaults = {
            types: 0,
            upload: undefined
        }
        var options = $.extend(defaults,options);
        return this.each(function (){
            var form = this;
            form = $(form);
            form.validatorForm({types:options.types}); //表单处理
            if ($(".ui-editor").length > 0){form.find('.ui-editor').editor(options.upload)}//编辑器
            if ($(".ui-mieditor").length > 0){form.find('.ui-mieditor').minieditor(options.upload)}//编辑器
            if ($(".ui-date").length > 0){form.find('.ui-date').layday()}   //时间选择器
            if ($(".ui-time").length > 0){form.find('.ui-time').laytime()}//时间选择器
            if ($(".ui-color").length > 0){form.find('.ui-color').color()}   //颜色选择器   
            if ($(".ui-upload").length > 0){$(".ui-upload").click(function(){$(this).popup(options.upload)})}//上传附件
        });
    };
    //表单验证
    $.fn.validatorForm = function (options){
        var defaults = {types:0}
        var options  = $.extend(defaults,options);
        return this.each(function (){
            var form = this;
            if (options.types == 0){
                $(form).Validform({
                    btnSubmit: ".submit",showAllError:false,tiptype:function(msg,o){if(o.type ==3){layer.tips(msg,'.submit')}},ajaxPost: true,postonce:true,ignoreHidden:true,
                    beforeSubmit:function(curform){
                        parent.layer.load(0,{shade:[0.2,'#000'],time:1500}); 
                    },
                    callback: function(data) {
                        var index = parent.layer.getFrameIndex(window.name); 
                        if (data.code == "200") {
                            parent.layer.alert(data.msg,{icon:1,closeBtn:0},function(index){
                                if(typeof data.parent != "undefined"){
                                    $.isEmptyObject(data.url) ? window.parent.location.reload(): window.parent.location.replace(data.url);
                                }else{
                                    $.isEmptyObject(data.url) ? window.location.reload(): window.location.replace(data.url);
                                }
                                parent.layer.close(index);
                            });
                        }else if(data.code == "302"){
                            if(typeof data.parent != "undefined"){
                                $.isEmptyObject(data.url) ? window.parent.location.reload(): window.parent.location.replace(data.url);
                            }else{
                                $.isEmptyObject(data.url) ? window.location.reload(): window.location.replace(data.url);
                            }
                            parent.layer.close(index);
                        } else {
                            parent.layer.alert(data.msg,{icon:5,closeBtn:0});
                        }
                    }
                })
            }else{
                $(form).Validform({btnSubmit: ".submit",showAllError:false,postonce:true,tiptype:function(msg,o){if(o.type ==3){layer.tips(msg,'.submit')}},ignoreHidden:true,beforeSubmit:function(curform){
                    parent.layer.load(0,{shade:[0.2,'#000'],time:1500}); 
                }})
            }
        });
    };
    //颜色
    $.fn.color = function (options){
        return this.each(function (){
            $(this).soColorPacker();
        });
    };
    //时间插件不带时间
    $.fn.layday = function () {
        this.each(function () {
            laydate.render({elem:this});
        });
    };
    //时间插件带时间
    $.fn.laytime = function () {
        this.each(function () {
            laydate.render({elem:this,'type':'datetime'});
        });
    };
    //编辑器调用
    $.fn.editor = function (uploads){
        var defaults = {uploadUrl:uploads}
        var options = $.extend(defaults,options);
        return this.each(function (){
            var id = this;
            var editorConfig = {
                allowFileManager : false,
                uploadJson:options.uploadUrl,
                urlType:"domain",
                items : ['source','plainpaste','|','fontsize','forecolor','fontname','hilitecolor','bold', 'italic','underline','|', 'justifyleft','justifycenter','justifyright','insertorderedlist','insertunorderedlist','|', 'image', 'multiimage','media','table','|','link','unlink','|','clearhtml','quickformat','|','fullscreen', 'preview'],
                afterBlur : function (){this.sync();},width :'100%'
            };
            editorConfig = $.extend(editorConfig,options.config);
            KindEditor.create(id,editorConfig);
        });
    };
    //编辑器调用
    $.fn.minieditor = function (uploads){
        var defaults = {uploadUrl:uploads}
        var options = $.extend(defaults,options);
        return this.each(function (){
            var id = this;
            var editorConfig = {
                allowFileManager : false,
                uploadJson:options.uploadUrl,
                urlType:"domain",
                items : ['plainpaste','|','fontsize','forecolor','fontname','hilitecolor','bold', 'italic','underline','|', 'justifyleft','justifycenter','justifyright','insertorderedlist','insertunorderedlist','|','link','unlink'],
                afterBlur : function (){this.sync();},width :'100%'
            };
            editorConfig = $.extend(editorConfig,options.config);
            KindEditor.create(id,editorConfig);
        });
    };
})(jQuery);